<template>
  <!-- 登录页面的整体盒子 -->
  <div class="login-container">
    <!-- 登录的盒子 -->
    <div class="login-box">
      <!-- 标题的盒子 -->
      <div class="title-box">爱尔眼科后台管理系统</div>
      <!-- 登录的表单区域 -->
      <el-form :model="loginForm" :rules="loginRules" ref="loginRef">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="loginFn"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import { LoginAPI } from "@/api";
import { mapMutations } from "vuex";

export default {
  created() {
    this.$store.dispatch("getuserInfoActions");
  },
  name: "my-login",
  data() {
    return {
      // 登录表单的数据对象
      loginForm: {
        username: "",
        password: "",
      },
      token: "",
      // 登录表单的验证规则对象
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: "用户名必须是1-10的字母数字",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "密码必须是6-15的非空字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    ...mapMutations(["updateToken"]),
    // 登录按钮->点击事件

    loginFn() {
      /**当点击提交按钮时执行 */
      this.$refs.loginRef.validate(async (valid) => {
        if (valid) {
          const url = "/areye/loginin";
          const params = `username=${this.loginForm.username}&password=${this.loginForm.password}`;
          await this.axios.post(url, params).then((res) => {
            let data = res.data;
            this.updateToken(data.token);
            localStorage.setItem("token", data.token);
            this.$router.push("/index");
            if (data.status == 0) {
              this.$message({
                showClose: true,
                message: "恭喜你，登录成功",
                type: "success",
              });
            }
          });
        } else {
          this.$message({
            showClose: true,
            message: "登录失败，请检查用户名或密码",
            type: "warning",
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  background: linear-gradient(to top, #42d1fd, #4183fb);
  width: 100vw;
  height: 100vh;

  .login-box {
    width: 800px;
    height: 470px;
    background-color: #fff;
    border-radius: 30px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-shadow: 10px 2px 10px 0px rgb(216, 216, 216);
    box-sizing: border-box;

    .title-box {
      margin-top: 40px;
      text-align: center;
      height: 60px;
      font-size: 30px;
    }

    ::v-deep .el-form {
      width: 60%;
      margin: 50px auto;
    }

    .el-input__inner {
    }
    .btn-login {
      width: 80%;
      margin-left: 50%;
      margin-top: 10%;
      transform: translateX(-50%);
      border-radius: 30px;
    }
  }
}
</style>